from selenium.webdriver.common.by import By
import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from web import DButils


class Mloan:
    def loan(self, driver, username, password, money, term):
        driver.find_element(By.ID, "username").send_keys(username)
        driver.find_element(By.ID, "password").send_keys(password)
        driver.find_element(By.ID, "login_btn").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[3]/a"))
        )
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[3]/a").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located(
                (By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[3]/ul/li[2]/a"))).click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.XPATH, "/html/body/div[1]/div/main/div/div/div/div/div[2]/div"))
        )
        driver.find_element(By.ID, 'amount').clear()
        driver.find_element(By.ID, 'amount').send_keys(money)
        driver.find_element(By.ID, 'term').clear()
        driver.find_element(By.ID, 'term').send_keys(term)
        driver.find_element(By.ID, "submit").click()
        WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located(
                (By.CSS_SELECTOR, "div.jconfirm-box.jconfirm-type-default")
            )
        )
        time.sleep(1)
        # 步骤2：点击确认按钮
        confirm_btn = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable(
                (By.CSS_SELECTOR, "div.jconfirm-box .btn-primary")
            )
        )
        confirm_btn.click()
        time.sleep(0.25)

    def loan_result(self, driver, username):
        sql = "select * from loan where userId=(select id from user where username=%s)"
        param = [username]
        text = driver.find_element(By.XPATH, "//div[@class='jconfirm-content']/div").text
        result = [text, len(DButils.select(sql, param))]
        return result

    def admin_loan(self, driver):
        driver.find_element(By.ID, "username").send_keys("admin")
        driver.find_element(By.ID, "password").send_keys('123456')
        driver.find_element(By.ID, "login_btn").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[5]/a").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "/html/body/div[1]/div/aside/div[2]/nav/ul/li[5]/ul/li[1]/a").click()
        time.sleep(1)
        (driver.find_element(By.XPATH,
                             "/html/body/div[1]/div/main/div/div/div/div/div[2]/div/table/tbody/tr/td[7]/button[1]")
         .click())
        time.sleep(1)
        (driver.find_element(By.XPATH,
                             "//div[@class='jconfirm-buttons']/button[@class='btn btn-primary']").click())
        time.sleep(1)

    def loan_adminText_result(self, driver):
        text = driver.find_element(By.XPATH, "//div[@class='jconfirm-content']/div").text
        result = text
        return result

    def loan_adminSQL_result(self, cardNum,username):
        sql = "select balance from bankcard where cardNum=%s and userId=(select id from user where username=%s)"
        param = [cardNum,username]
        result = int(DButils.select(sql, param)[0][0])
        return result
